package znet

import (
	"MyDemo/MyZinx/ziface"
	"fmt"
)

/*
	消息处理模块的实现
*/

type MsgHandler struct {
	Apis map[uint32]ziface.IRouter
}

func NewMsgHandler() *MsgHandler {
	return &MsgHandler{
		Apis: make(map[uint32]ziface.IRouter),
	}
}

func (mh *MsgHandler) DoMsgHandler(request ziface.IRequest) {
	handler, ok := mh.Apis[request.GetMsgId()]
	if !ok {
		fmt.Println("Api msgId=", request.GetMsgId(), " is not found! Need register")
	}

	handler.PreHandle(request)
	handler.Handle(request)
	handler.PostHandle(request)
}

func (mh *MsgHandler) AddRouter(msgId uint32, router ziface.IRouter) {
	if _, ok := mh.Apis[msgId]; ok {
		panic("Repeat api, msgId=" + string(msgId))
	}
	mh.Apis[msgId] = router
	fmt.Println("Add api msgId=", msgId, " succ!")
}
